<div class="layui-layout layui-layout-admin" style="padding-left: 20px;">
    <div class="layui-row" style="margin-top: 20px;">
        <div class="layui-col-xs6">
            <div class="layui-btn-group">
                <a class="layui-btn" data-type="tabAdd" href="/task/add">新增</a>
                <button class="layui-btn batch" data-type="batchstart" >启动</button>
                <button class="layui-btn layui-btn-danger batch" data-type="batchpause" >暂停</button>cron
            </div>
        </div>
        <div class="layui-col-xs6 search_text">
            <form class="layui-form" action="" onsubmit="javascript:return false;">
                <div class="demoTable">

                    <div class="layui-inline" style="width: 40%">

                            {{/*<label class="layui-form-label">搜索选择框</label>*/}}
                            <div class="layui-input-inline" style="text-align: left">
                                <select name="modules" lay-verify="required" lay-filter="select_group" id="group_id" lay-search="">
                                    <option value="">直接选择或搜索选择</option>
                                    {{range $k, $v := .taskGroup}}
                                        <option value="{{$k}}">{{$v}}</option>
                                    {{end}}
                                </select>
                            </div>

                        {{/*<input class="layui-input" name="taskName" id="taskName" autocomplete="off" placeholder="任务名称" >*/}}
                    </div>
                    <button class="layui-btn" data-type="reload" id="reload" style="display: none">查询</button>
                </div>
            </form>
            
        </div>
    </div>

    <table class="layui-hide" id="table_list" lay-filter="table_filter">
    </table>

    <script type="text/html" id="bar">
        {{/*<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="status">启|停</a>*/}}
        {{/*<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>*/}}
        <a class="layui-btn layui-btn-xs " lay-event="detail">详细</a>
        <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="run">测试</a>
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="log">日志</a>
    </script>
</div>
<script>

    layui.use(['table','form','element'], function(){
        var table = layui.table;
        var form = layui.form;
        var element = layui.element;
        var error_info = "{{.flash.error}}";
        if(error_info){
            layer.msg(error_info,{icon: 2,shade:0.3},function () {
                window.history.go(-1)
            })
            return;
        }
        //方法级渲染
        table.render({
            elem: '#table_list'
            ,url: '/task/table'
            ,cols: [[
                {checkbox: true, fixed: true},
                {field:'id', title: 'ID', align:'center', width:50,sort: true}
                ,{field:'task_name',width:300, title: '任务名称'}
                ,{field:'description',title: '任务说明'}
                ,{field:'next_time', width:170,title: '下次执行时间'}
                ,{field:'pre_time', title: '上次执行时间'}
                ,{field:'execute_times', title: '次数'}
                ,{fixed: 'right', width:150, align:'center', title:'操作', toolbar: '#bar'}
            ]]
            ,id: 'listReload'
            ,page: true
            ,limit:20
            ,limits: [20,40,80]
            ,height: "full-130"
        });

        var $ = layui.$, active = {
            reload: function(){
                table.reload('listReload', {
                    where: {
                        group_id: $('#group_id').val(),
                    }
                });
            }
        };

        form.on('select(select_group)', function(data){
            // console.log(data.elem); //得到select原始DOM对象
            // console.log(data.value); //得到被选中的值
            // console.log(data.othis); //得到美化后的DOM对象
            $("#reload").click()
        });

        //监听工具条
        table.on('tool(table_filter)', function(obj){
            var data = obj.data;
            if(obj.event === 'edit'){
                window.location.href="/task/edit?id="+data.id
            } else if(obj.event === 'log'){
                window.location.href="/tasklog/list?task_id="+data.id;
            }else if (obj.event==='detail'){
                window.location.href='/task/detail?id='+data.id;
            }else if (obj.event==='run'){
                layer.confirm("测试执行任务，注意不要超时，请确认执行", {icon: 3, title:'提示'}, function(index){
                    layer.load();
                    $.post('/task/ajaxrun', {id:data.id}, function (out) {
                        if (out.status == 0) {
                            layer.msg("操作成功",{icon: 1,shade:0.3,time:1000},function () {
                                window.location.href="/tasklog/list?task_id="+data.id
                            })
                        } else {
                            layer.msg(out.message)
                            return
                        }
                    }, "json");
                    setTimeout(function(){
                        layer.closeAll('loading');
                    }, 2000);
                    layer.close(index);
                });
            }else if (obj.event==='status'){
                layer.msg("状态")
            }else{
                layer.msg('操作不存在')
            }
        });
        
        $(".batch").on('click',function (obj) {
            var action = $(this).attr('data-type');
            var checkStatus = table.checkStatus('listReload');
            var data = checkStatus.data;
            if(data.length<1){
                layer.msg("请选择操作数据");
                return;
            }

            var  msg = "确认启动这些任务吗";
            if (action=='batchpause'){
                msg = "确定暂定这些任务吗";
            }

            layer.confirm(msg, {icon: 3, title:'提示'}, function(index){
                var ids = "";
                $.each(data,function (k,v) {
                    ids += v.id+",";
                });
                ids = ids.substr(0,ids.length-1);

                layer.load();
                $.post('/task/ajax'+action, {"ids":ids}, function (out) {
                    if (out.status == 0) {
                        layer.msg("操作成功",{icon: 1,shade:0.3,time:1000},function () {
                            $('#reload').click();
                        })
                    } else {
                        layer.msg(out.message)
                        return
                    }
                }, "json");
                setTimeout(function(){
                    layer.closeAll('loading');
                }, 2000);


                layer.close(index);
            });
            return ;
        })

        $('.demoTable .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });

</script>